package com.example.myfirstapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 *  圆形进度条
 */
public class CirclePgBar extends View {
    private Paint mBackPaint;
    private Paint mFrontPaint;
    private Paint mTextPaint;

    private float mStrokeWidth = 20;
    private float mHalfStrokeWidth = mStrokeWidth/2;
    private float mRadius = 200;
    private RectF mRect;
    private int mProgress = 0;

    private int mTargetProgress = 100;
    private int mMax = 100;
    private int mWidth;
    private int mHeight;


    public CirclePgBar(Context context) {
        super(context);
        init();
    }

    public CirclePgBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CirclePgBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     *  初始化
     */
    private void init() {
        mBackPaint = new Paint();
        mBackPaint.setColor(getResources().getColor(R.color.color29));
        mBackPaint.setAntiAlias(true); //抗锯齿
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mStrokeWidth);

        mFrontPaint = new Paint();
        mFrontPaint.setColor(getResources().getColor(R.color.colorFe));
        mFrontPaint.setAntiAlias(true);
        mFrontPaint.setStyle(Paint.Style.STROKE);
        mFrontPaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.colorFe));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(80);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    /**
     * 重写测量大小的onMeasure方法
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getRealSize(widthMeasureSpec);
        mHeight = getRealSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    /**
     * 重写View的onDraw方法
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        initRect();
        float angle = mProgress / (float)mMax * 360;
        canvas.drawCircle(mWidth/2, mHeight/2, mRadius, mBackPaint);
        canvas.drawArc(mRect, -90, angle, false, mFrontPaint);
        canvas.drawText(mProgress + "%", mWidth/2 + mHalfStrokeWidth, mHeight/2 + mHalfStrokeWidth*2, mTextPaint);
        if(mProgress < mTargetProgress) {
            mProgress += 1;
            invalidate();
        }
    }

    /**
     *
     */
    private void initRect() {
       if(mRect == null) {
           mRect = new RectF();
           int viewSize = (int)(mRadius * 2);
           int left = (mWidth - viewSize)/2;
           int top = (mHeight - viewSize)/2;
           int right = left + viewSize;
           int bottom = top + viewSize;
           mRect.set(left, top, right ,bottom);
       }
    }

    /**
     *
     * @param measureSpec
     * @return
     */
    private int getRealSize(int measureSpec) {
        int result = 1;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) {
            result = (int)(mRadius * 2 + mStrokeWidth);
        } else {
            result = size;
        }

        return result;
    }

}
